<%inherit file="/base/user.html"/>

<% 
from gridmonitor.lib.nagios_utils import get_nagios_summary
from gridmonitor.lib.nagios_utils import get_hostnames_from_group_tag
DELIMITER = '-1-'

cores_tag = config['nagios_core']
ces_tag = config['nagios_ces']
core_hosts = get_hostnames_from_group_tag(cores_tag)
ces_hosts = get_hostnames_from_group_tag(ces_tag)

cores = get_nagios_summary(core_hosts, dates2utc = True)
ces = get_nagios_summary(ces_hosts, dates2utc = True)

state_map = {'up': 'ok_status', 'ok': 'ok_status', 'scheduleddown': 'ok_status',
            'down': 'error_status', 'error': 'error_status', 'critical': 'error_status',
            'warn': 'warn_status',  
            'unknown': 'undef_status', 'unknown': 'undef_status'}
%>

<p>
The following two tables display the output of Nagios, a monitoring system, 
which runs (local) probes on the listed hosts at regular time intervals. 
The first table lists the output of the core Grid services of the ${config['gridname']}, 
while the second table the output of the probes (called plugins) of all front-ends of
the Grid. <br /> <br/>
<b>Notice</b>, you can click on the hostname to get some additional information about e.g.the host's 
response time.
</p>


<a name="core"></a> 
<table width="920" id="nagios_core_services" summary="Display of nagios checks for Grid core services and hosts">
    <caption> Core Services of ${config['gridname']}</caption>
    <tr> 
        <th class="gm"> Host</th> <!--Host checktime and performance display by qTip --> 
        <th class="gm"> Service</th>
        <th class="gm"> Last Check </th>
        <th class="gm"> Plugin Output</th>
        <th class="gm"> Performance Data</th>
    </tr>

%for status in ['scheduleddown','down','up','unknown']:
    %for host_info in cores['host_summary'][status]:
        <% 
        hostname = host_info['hostname']  
        h_last_check =  host_info['date']
        h_output = host_info['output']
        key = hostname.replace('.', DELIMITER) 
        if (status == 'scheduleddown'):
            n_services = 1
        else:
            n_services = len(cores['details'][key])
        first_flg = True
        %> 
        
        <tr>
        <td rowspan=${n_services} />    <!-- HOST INFO -->
            <!-- qtip display info -->
            <table class="nagios_host_details" summary="Host details infobox" status = '${state_map[status]}' hn='${hostname}'>
                <tr><td>Hostname</td> <td>${hostname}</td> </tr>
                <tr><td>Last check </td> <td>${h_last_check} </td> </tr>
                <tr><td>Ouptput </td> <td>${h_output} </td> </tr>
            </table>
            <!-- end qtip -->
        </td>

        %if (status == 'scheduleddown'):
            <td colspan=4> <span class='ok_status'> Scheduled downtime: ${h_output}</span> </td> </tr> 
        %else:   
            %for _service, s_details in cores['details'][key].items():
                %if not first_flg :
                    <tr>
                %endif
                <% 
                service_name = cores['service_name_map'][_service] 
                first_flg = False
                s_status = s_details['status']
                %>

                <td>  <span class="${state_map[s_status]}"> ${service_name}</span></td>
                <td>  <span class="${state_map[s_status]}"> ${s_details['last_check']}</span></td>
                <td>  <span class="${state_map[s_status]}"> ${s_details['output']}</span></td>
                <td>  <span class="${state_map[s_status]}"> ${s_details['perfdata']}</span></td>
               </tr> 

            %endfor
        %endif
    %endfor 
%endfor 

</table>

<!-- CES TABLE -->
<a name="ces"></a>
<table width="920" id="nagios_ces_services" summary="Display of nagios checks for all moniotre Grid Computing element  services and hosts">
    <caption> Grid Computing Elements  Services of ${config['gridname']}</caption>
    <tr> 
        <th class="gm"> Host</th> <!--Host checktime and performance display by qTip --> 
        <th class="gm"> Service</th>
        <th class="gm"> Last Check </th>
        <th class="gm"> Plugin Output</th>
        <th class="gm"> Performance Data</th>
    </tr>

%for status in ['scheduleddown','down','up','unknown']:
    %for host_info in ces['host_summary'][status]:
        <% 
        hostname = host_info['hostname']  
        h_last_check =  host_info['date']
        h_output = host_info['output']
        key = hostname.replace('.', DELIMITER) 
        if (status == 'scheduleddown'):
            n_services = 1
        else:
            n_services = len(ces['details'][key])
        first_flg = True
        %> 
        
        <tr>
        <td rowspan=${n_services} />    <!-- HOST INFO -->
            <!-- qtip display info -->
            <table class="nagios_host_details" summary="Host details infobox" status = '${state_map[status]}' hn='${hostname}'>
                <tr><td>Hostname</td> <td>${hostname}</td> </tr>
                <tr><td>Last chedk </td> <td>${h_last_check} </td> </tr>
                <tr><td>Ouptput </td> <td>${h_output} </td> </tr>
            </table>
            <!-- end qtip -->
        </td>
        
        %if (status == 'scheduleddown'):
            <td colspan=4> <span class='ok_status'> Scheduled downtime: ${h_output}</span> </td> </tr> 
        %else:   
            %for _service, s_details in ces['details'][key].items():
                %if not first_flg :
                    <tr>
                %endif
                <% 
                service_name = ces['service_name_map'][_service] 
                first_flg = False
                s_status = s_details['status']
                %>

                <td>  <span class="${state_map[s_status]}"> ${service_name}</span></td>
                <td>  <span class="${state_map[s_status]}"> ${s_details['last_check']}</span></td>
                <td>  <span class="${state_map[s_status]}"> ${s_details['output']}</span></td>
                <td>  <span class="${state_map[s_status]}"> ${s_details['perfdata']}</span></td>
               </tr> 

            %endfor
        %endif
    %endfor 
%endfor 



</table>

<script type="text/javascript"> 
$('table.nagios_host_details').each(function() {
   
    var hostname = $(this).attr('hn');
    var _status = $(this).attr('status');    

    $(this).qtip({
        content : {
            text: $(this).html(),
            title: {
                text:'Host Details'
                }
        },
        show : {
                 when: { event:'click'}
               },
        hide : {
                when: {event: 'unfocus'},
                delay: 500
                },
        style: {
            width:{
                max:400,
                min:120
            },
        padding: 5,
        background:'#FFFFFF',
        color: '#055670',
        textAlign: 'left',
        border: {
             width: 1,
            radius: 5,
            color: '#4682B4'
        },
        tip: {
            corner:'leftTop'
        },

      name: 'blue' // Inherit the rest of the attributes from the preset dark style
    }

    }).html('<span class='+ _status + '>' + hostname + '</span>').show();
});

</script>

<%def name="js()">        
    ${parent.js()}
    ${self.js_link("/js/qtip/jquery.qtip-1.0.0-rc3.min.js")}
</%def>
